package edu.id.idprogrammingproject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Hangman {

	public static void main(String args []) throws FileNotFoundException{

		System.out.println("Welcome to Hangman. You have eight lives to guess the word");
		Scanner myScanner = new Scanner(System.in);

		System.out.println("Do you want one player (press \"1\") or two player (press \"2\")");
		String answer = myScanner.nextLine();
		String word ="";
		
		if(answer.equals("2")){
			System.out.println("Enter a word");	
			word = myScanner.nextLine();
			
		}
		if(answer.equals("1")){
			Scanner dict = new Scanner(new File("dictionary.txt"));
			int x = (int) (Math.random() * 846);
			for(int i = 0; i < x; i++){
				word = dict.next();
			}

		}
		int length = word.length();
		char[]arr = word.toCharArray();
		int lives = 8;
		int counter = 0;
		String guessed = "";

		for(int i = 0; i < 10; i++){	
			System.out.println();
		}

		for(int a = 0; a < length; a++){	
			arr[a] = '_';
			System.out.print(arr[a] + " ");
		}

		for(int i = 0; i < 26; i++){	
			System.out.println("\nEnter letter");
			String letter = myScanner.nextLine();

			if(word.indexOf(letter) == -1){	
				String a = " ( )\n";
				String b = "-";
				String c = " |";
				String d = " -\n";
				String e = "  | \n";
				String f = " / ";
				String g = "\\";

				if(guessed.contains(letter)){
					for(int b1 = 0; b1 < length; b1++){
						if (letter.charAt(0) == word.charAt(b1)){	
							char c1 = letter.charAt(0);
							arr[b1] = c1;
						}
						System.out.print(arr[b1] + " ");
						}
					guessed += "";
					lives -= 0;
					System.out.println("\n**You have already entered this letter**");

				}
				else{
					guessed += letter;
					lives--;
					for(int b1 = 0; b1 < length; b1++){
						if (letter.charAt(0) == word.charAt(b1)){	
							char c1 = letter.charAt(0);
							arr[b1] = c1;

						}
						System.out.print(arr[b1] + " ");
					}

				}
				System.out.println("\nWRONG");
				System.out.println("\n*Already guessed*: " + guessed);


				if(lives == 7){ 
					System.out.println(a);
				}
				if(lives == 6){
					System.out.println(a + b);
				}
				if(lives == 5){
					System.out.println(a + b + c);
				}
				if(lives == 4){
					System.out.println(a + b + c + d);
				}
				if(lives == 3){
					System.out.println(a + b + c + d + e);
				}
				if(lives == 2){
					System.out.println(a + b + c + d + e + f);
				}
				if(lives == 1){
					System.out.println(a+ b + c + d + e + f + g);
				}

				if(lives == 1){
					System.out.println("YOU LOSE!!!");
					System.out.println("The word is " + word);
					break;
				}
			}
			else
			{
				System.out.println("CORRECT");
				
				for(int w = 0; w < length; w++){
					if (letter.charAt(0) == word.charAt(w)){	
						char c = letter.charAt(0);
						arr[w] = c;
						if(!guessed.contains(letter)){
							counter++;
						}	
					}
					
					System.out.print(arr[w] + " ");
				}
				if(!guessed.contains(letter)){
							guessed += letter;
						}
				if(counter == length){
					System.out.println("\nYOU WIN!!!");	
					break;

				}
				System.out.println("\n*Already guessed*: " + guessed);
			}
		}
	}
}